<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> Tencent - Upload </title>
    <script src = "../../js/jquery-3.1.1.min.js"></script>
    <script src = "./swfobject.js"></script>
    <script src = "./tencent.js"></script>
    <script src = "../uploader.js"></script>
   <style type = "text/css">
	.container{
		position: absolute; 
		height: 40px; 
		width: 300px; 
		color: Silver;
    	border-width: 1px; 
    	border-style: Solid;
	}
	.progressBar{
		position: absolute; 
		height: inherit; 
		width: 0%; 
		background-color: gray;
	}

	.progressContent{
		position: absolute; 
		height: inherit; 
		width: 100%;
		text-align:center; 
		font-size:32px;
	}
	#success-log{
		float: right;
	}
</style>
</head>
<body>
	<p>File：<input type="file" id="file-Id" value="upload-file" /></p>

	<p>Base64：<input type="button" id="image-Id" value="Base64"/></p>

	<div class="container" id="container">
        <div class = "progressBar" id= "progressBar"></div>
        <div class = "progressContent" id = "progressContent">
        </div>
    </div> 
    <div id="success-log"></div>
    <object id="qs" width="0" height="0" type="application/x-shockwave-flash" data="./Somethingtest.swf" style="visibility: visible;"></object>
</body>
<script>

/**
腾讯官方文档: https://www.qcloud.com/document/product/436/6054

 var options = {
	 	domain			: '',											// default : '' ,必须设置文件服务器地址。
	  	file_data_name	: 'file',										// default : file , 文件对象的 key 。
	 	base64_size		: 3,											// default : 3 单位 MB 。
		chunk_size		: 3,											// default : 3 单位 MB 。
		headers			: { Content-Type : 'multipart/form-data'},		// default : { Content-Type : 'multipart/form-data'} ,增加 requestHeader 需扩展。 
	  	multi_parmas	: { },											// default : {} 扩展上传属性 。
	  	query			: { },											// default : {}	扩展 url 参数 e.g. http://rongcloud.cn?name=zhangsan 。
	  	support_options : true,											// default : true, 文件服务器不支持 OPTIONS 请求需设置为 false。
		data 			: dataType.form,
		refreshSign		: function(cb){}
  	};
*/

//-----FILE----------------------------------------------------------------------------------------------------------
var callback = {
		onError	: function (errorCode) { 
			console.log(errorCode);
		},
		onProgress : function (loaded, total) {
			var percent = Math.floor(loaded/total*100);
			var progressBar 	= document.getElementById('progressBar'), 
				progressContent = document.getElementById('progressContent');
				progressBar.style.width = percent + '%';
        		progressContent.innerHTML = percent + "%";
		},
		onCompleted : function (data) { 
			document.getElementById('success-log').innerHTML += JSON.stringify(data) + '</br>';
		} 
 };

var fileConfig = { 
			domain: 'http://web.file.myqcloud.com/files/v1/10011010',
		 	multi_parmas : {op: 'upload', insertOnly:0},
		 	file_data_name : 'fileContent',
		 	support_options:false,
		 	chunk_size:1048576,
		 	refreshSign:function(cb){
				$.ajax({
	                type: "GET",
	                url: "http://10.126.85.204:9528/uploadandsign?bid=3&passwd=test&bucket_name=test&mediatype=1&expired=99999999999999",
	                dataType: "text",
	                success: function (data) {
	                    var data = JSON.parse(data);
	                	cb({path:'http://web.file.myqcloud.com/files/v1/10011010' + data.path, sign:data.sign});
	                }
	            });
		 	}
		 };

var fileUpload = UploadFile.init(fileConfig);

var file = document.getElementById("file-Id");
file.onchange = function(){
	fileUpload.upload(file.files[0], callback);
};

//----IMAGE-----------------------------------------------------------------------------------------------------------

var image = document.getElementById('image-Id');

var imgConfig = { 
			domain: 'http://upload.58cdn.com.cn/json?rand=0.1928/',
		 	data:UploadFile.dataType.json,
		 	file_data_name:'Pic-Data',
		 	multi_parmas:{
		 		"Pic-Size": "0 * 0",
                "Pic-Encoding": "base64",
                "Pic-Path": "/p1/"
		 	}
		 };

var imgUpload = UploadFile.init(imgConfig);
	image.onclick = function(){
		imgUpload.upload(base64, callback);
	};
var base64 = "/9j/4AAQSkZJRgABAQAAAQABAAD....";

</script>

</html>